home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / system-tools / tinymeter / source / tinymeter_prefs / startprefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  2.6 KB  |  108 lines

  1. #include <exec/types.h>
  2. #include <graphics/gfx.h>
  3. #include <libraries/asl.h>
  4. #include <libraries/gadtools.h>
  5. #include <dos/dos.h>
  6. #include <dos/dostags.h>
  7. #include <clib/alib_protos.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <iff.h>
  11. #include <exec/memory.h>
  12. #include <exec/execbase.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/IntuitionBase.h>
  15. #include <rexx/errors.h>
  16. #include <clib/rexxsyslib_protos.h>
  17. #include <proto/rexxsyslib.h>
  18. #include <WBStart.h>
  19. #include <workbench/startup.h>
  20. #include <workbench/workbench.h>
  21.  
  22. int main()
  23. {
  24.     BPTR fl;
  25.     struct WBStartMsg msg;
  26.     struct MsgPort *mp,*hp;
  27.  
  28.     if (!(mp=(struct MsgPort *)CreateMsgPort())) exit(20);
  29.  
  30.     fl=(BPTR)CurrentDir(NULL);
  31.     msg.wbsm_Msg.mn_Node.ln_Pri=0;
  32.     msg.wbsm_Msg.mn_ReplyPort=mp;
  33.     msg.wbsm_DirLock=fl;
  34.     msg.wbsm_Stack=8192;
  35.     msg.wbsm_Prio=0;
  36.     msg.wbsm_NumArgs=0;
  37.     msg.wbsm_ArgList=NULL;
  38.  
  39.     msg.wbsm_Name="SYS:Prefs/TinyMeterPrefs";
  40.  
  41.     /* Try to send a message to the WBStart-Handler */
  42.     Forbid();
  43.     hp=(struct MsgPort *)FindPort(WBS_PORTNAME);
  44.     if (hp) PutMsg(hp,(struct Message *) &msg);
  45.     Permit();
  46.  
  47.     /* No WBStart-Handler, try to start it! */
  48.     if (!hp)
  49.     {
  50.         BPTR ifh=Open("NIL:",MODE_NEWFILE);
  51.         BPTR ofh=Open("NIL:",MODE_OLDFILE);
  52.  
  53.         /* Start handler */
  54.         if (SystemTags(WBS_LOADNAME,SYS_Input,ifh,
  55.                                     SYS_Output,ofh,
  56.                                     SYS_Asynch,TRUE,
  57.                                     SYS_UserShell,TRUE,
  58.                                     NP_ConsoleTask,NULL,
  59.                                     NP_WindowPtr,NULL,
  60.                                     TAG_DONE)!=-1)
  61.         {
  62.             int i;
  63.  
  64.             /* Handler started, try to send message (Retry up to 10 seconds) */
  65.             for (i=0; i<20; i++)
  66.             {
  67.                 /* Try to send message */
  68.                 Forbid();
  69.                 hp=(struct MsgPort *)FindPort(WBS_PORTNAME);
  70.                 if (hp) PutMsg(hp,(struct Message *) &msg);
  71.                 Permit();
  72.  
  73.                 /* Message sent? Yes, leave loop */
  74.                 if (hp) break;
  75.  
  76.                 /* No, wait 1/2 second */
  77.                 Delay(25);
  78.             }
  79.         }
  80.         else
  81.         {
  82.             /* Handler not started, close file handles */
  83.             Close(ifh);
  84.             Close(ofh);
  85.         }
  86.     }
  87.  
  88.     /* Could we send the message? */
  89.     if (hp)
  90.     {
  91.         /* Get reply message */
  92.         WaitPort(mp);
  93.         GetMsg(mp);
  94.         return(0);
  95.     }
  96.     else
  97.     {
  98.         /* Oops. ERROR! */
  99.         return(1);
  100.     }
  101.  
  102.     /* Free resources */
  103.     CurrentDir(fl);
  104.     DeleteMsgPort(mp);
  105.     return(0L);
  106. }
  107.  
  108.